package com.savitar.account.controller;

import com.savitar.account.entity.Account;
import com.savitar.account.service.AccountService;
import com.savitar.common.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService service;

    @GetMapping("/page")
    public ResultBean page(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                           @RequestParam(value = "username", required = false) String username,
                           @RequestParam(value = "name", required = false) String name) {
        try {
            return ResultBean.ok(service.page(pageNum, pageSize,username,name));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @PostMapping
    public ResultBean create(@RequestBody Account createVo) {
        try {
            return ResultBean.ok(service.create(createVo));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @PutMapping
    public ResultBean update(@RequestBody Account updateVo) {
        try {
            return ResultBean.ok(service.update(updateVo));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResultBean deleteById(@PathVariable String id) {
        try {
            return ResultBean.ok(service.deleteById(id));
        } catch (Exception e) {
            return ResultBean.err(e.getMessage());
        }
    }

}
